// 星球类（带表面细节 + 移动效果 + 星环）
import { TimeManager } from './TimeManager.js';
export class Planet {
    constructor(x, y, radius, baseColor, moveSpeed, hasRings) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.baseColor = baseColor;
        this.rotation = 0;
        this.rotationSpeed = 0.06; // 0.001 * 60 = 0.06弧度/秒
        this.craters = this.generateCraters();
        this.atmosphere = Math.random() > 0.5;
        this.moveSpeed = moveSpeed * 60; // 转换为像素/秒
        this.hasRings = hasRings; // 是否有星环
        
        // 星环属性
        if (this.hasRings) {
            this.ringInnerRadius = this.radius * 1.2;
            this.ringOuterRadius = this.radius * 1.8;
            this.ringColor = this.getRingColor();
            this.ringRotation = 0;
            this.ringRotationSpeed = 0.03; // 0.0005 * 60 = 0.03弧度/秒
        }
    }
    
    generateCraters() {
        const craters = [];
        const craterCount = Math.floor(Math.random() * 8) + 3;
        
        for (let i = 0; i < craterCount; i++) {
            const angle = Math.random() * Math.PI * 2;
            const distance = Math.random() * this.radius * 0.7;
            craters.push({
                x: Math.cos(angle) * distance,
                y: Math.sin(angle) * distance,
                radius: Math.random() * this.radius * 0.15 + 5,
                depth: Math.random() * 0.3 + 0.2
            });
        }
        
        return craters;
    }
    
    getRingColor() {
        const ringColors = [
            '#ffd700', // 金色
            '#c0c0c0', // 银色
            '#daa520', // 暗金色
            '#f5deb3', // 小麦色
            '#e6e6fa'  // 薄紫色
        ];
        return ringColors[Math.floor(Math.random() * ringColors.length)];
    }
    
    update(deltaTime) {
        // 自转效果（基于时间）
        this.rotation = TimeManager.updatePhase(this.rotation, deltaTime, this.rotationSpeed);
        
        // 星环旋转（如果有星环）
        if (this.hasRings) {
            this.ringRotation = TimeManager.updatePhase(this.ringRotation, deltaTime, this.ringRotationSpeed);
        }
        
        // 移动效果（向下移动，模拟飞行前进）
        this.y += this.moveSpeed * (deltaTime / 1000);
        
        // 当星球移出屏幕底部时，重新在顶部出现
        if (this.y > 800 + this.radius * 2) {
            this.y = -this.radius * 2;
            this.x = Math.random() * 1200;
        }
    }
    
    render(ctx) {
        ctx.save();
        ctx.translate(this.x, this.y);
        
        // 绘制星环后半部分（在星球后面）
        if (this.hasRings) {
            this.drawRings(ctx, 'back');
        }
        
        ctx.rotate(this.rotation);
        
        // 大气层效果
        if (this.atmosphere) {
            const atmosphereGradient = ctx.createRadialGradient(
                0, 0, this.radius * 0.8,
                0, 0, this.radius * 1.3
            );
            const baseColor = this.hexToRgb(this.baseColor);
            atmosphereGradient.addColorStop(0, `rgba(${baseColor.r}, ${baseColor.g}, ${baseColor.b}, 0)`);
            atmosphereGradient.addColorStop(0.7, `rgba(${baseColor.r}, ${baseColor.g}, ${baseColor.b}, 0.2)`);
            atmosphereGradient.addColorStop(1, `rgba(${baseColor.r}, ${baseColor.g}, ${baseColor.b}, 0)`);
            
            ctx.fillStyle = atmosphereGradient;
            ctx.beginPath();
            ctx.arc(0, 0, this.radius * 1.3, 0, Math.PI * 2);
            ctx.fill();
        }
        
        // 星球主体渐变
        const planetGradient = ctx.createRadialGradient(
            -this.radius * 0.3, -this.radius * 0.3, 0,
            0, 0, this.radius
        );
        planetGradient.addColorStop(0, this.lightenColor(this.baseColor, 0.4));
        planetGradient.addColorStop(0.6, this.baseColor);
        planetGradient.addColorStop(1, this.darkenColor(this.baseColor, 0.3));
        
        ctx.fillStyle = planetGradient;
        ctx.beginPath();
        ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
        ctx.fill();
        
        // 绘制陨石坑
        this.craters.forEach(crater => {
            const craterGradient = ctx.createRadialGradient(
                crater.x, crater.y, 0,
                crater.x, crater.y, crater.radius
            );
            craterGradient.addColorStop(0, this.darkenColor(this.baseColor, crater.depth));
            craterGradient.addColorStop(1, this.baseColor);
            
            ctx.fillStyle = craterGradient;
            ctx.beginPath();
            ctx.arc(crater.x, crater.y, crater.radius, 0, Math.PI * 2);
            ctx.fill();
        });
        
        // 阴影效果
        const shadowGradient = ctx.createRadialGradient(
            this.radius * 0.3, this.radius * 0.3, 0,
            0, 0, this.radius
        );
        shadowGradient.addColorStop(0, 'rgba(0, 0, 0, 0)');
        shadowGradient.addColorStop(0.7, 'rgba(0, 0, 0, 0.18)');
        shadowGradient.addColorStop(1, 'rgba(0, 0, 0, 0.37)');
        
        ctx.fillStyle = shadowGradient;
        ctx.beginPath();
        ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
        ctx.fill();
        
        // 重置旋转，绘制星环前半部分（在星球前面）
        ctx.rotate(-this.rotation);
        
        // 绘制星环前半部分（在星球前面）
        if (this.hasRings) {
            this.drawRings(ctx, 'front');
        }
        
        ctx.restore();
    }
    
    lightenColor(color, amount) {
        const num = parseInt(color.replace('#', ''), 16);
        const r = Math.min(255, Math.floor((num >> 16) + 255 * amount));
        const g = Math.min(255, Math.floor((num >> 8 & 0x00FF) + 255 * amount));
        const b = Math.min(255, Math.floor((num & 0x0000FF) + 255 * amount));
        return `#${(r << 16 | g << 8 | b).toString(16).padStart(6, '0')}`;
    }
    
    darkenColor(color, amount) {
        const num = parseInt(color.replace('#', ''), 16);
        const r = Math.max(0, Math.floor((num >> 16) * (1 - amount)));
        const g = Math.max(0, Math.floor((num >> 8 & 0x00FF) * (1 - amount)));
        const b = Math.max(0, Math.floor((num & 0x0000FF) * (1 - amount)));
        return `#${(r << 16 | g << 8 | b).toString(16).padStart(6, '0')}`;
    }
    
    hexToRgb(hex) {
        const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        return result ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16)
        } : {r: 255, g: 255, b: 255};
    }
    
    drawRings(ctx, part = 'full') {
        if (!this.hasRings) return;
        
        ctx.save();
        ctx.rotate(this.ringRotation);
        
        // 创建星环渐变
        const ringGradient = ctx.createRadialGradient(
            0, 0, this.ringInnerRadius,
            0, 0, this.ringOuterRadius
        );
        
        const ringColor = this.hexToRgb(this.ringColor);
        ringGradient.addColorStop(0, `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0)`);
        ringGradient.addColorStop(0.3, `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0.6)`);
        ringGradient.addColorStop(0.7, `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0.8)`);
        ringGradient.addColorStop(1, `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0)`);
        
        // 绘制星环（使用椭圆效果）
        ctx.fillStyle = ringGradient;
        ctx.beginPath();
        
        // 使用椭圆效果让星环看起来有立体感
        ctx.scale(1, 0.3); // 压缩Y轴，制造椭圆效果
        
        if (part === 'full') {
            // 绘制完整星环
            ctx.arc(0, 0, this.ringOuterRadius, 0, Math.PI * 2);
            ctx.arc(0, 0, this.ringInnerRadius, 0, Math.PI * 2, true);
        } else if (part === 'back') {
            // 绘制星环后半部分（下半部分）
            ctx.arc(0, 0, this.ringOuterRadius, 0, Math.PI);
            ctx.arc(0, 0, this.ringInnerRadius, Math.PI, 0, true);
        } else if (part === 'front') {
            // 绘制星环前半部分（上半部分）
            ctx.arc(0, 0, this.ringOuterRadius, Math.PI, 2 * Math.PI);
            ctx.arc(0, 0, this.ringInnerRadius, 2 * Math.PI, Math.PI, true);
        }
        
        ctx.fill();
        
        // 添加星环的细节线条
        if (part === 'full' || part === 'back') {
            ctx.strokeStyle = `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0.4)`;
            ctx.lineWidth = 0.5;
            for (let i = 0; i < 3; i++) {
                const radius = this.ringInnerRadius + (this.ringOuterRadius - this.ringInnerRadius) * (i + 1) / 4;
                ctx.beginPath();
                if (part === 'back') {
                    ctx.arc(0, 0, radius, 0, Math.PI);
                } else {
                    ctx.arc(0, 0, radius, 0, Math.PI * 2);
                }
                ctx.stroke();
            }
        }
        
        // 前半部分的细节线条（透明度更高）
        if (part === 'front') {
            ctx.strokeStyle = `rgba(${ringColor.r}, ${ringColor.g}, ${ringColor.b}, 0.7)`;
            ctx.lineWidth = 0.5;
            for (let i = 0; i < 3; i++) {
                const radius = this.ringInnerRadius + (this.ringOuterRadius - this.ringInnerRadius) * (i + 1) / 4;
                ctx.beginPath();
                ctx.arc(0, 0, radius, Math.PI, 2 * Math.PI);
                ctx.stroke();
            }
        }
        
        ctx.restore();
    }
}